<template>
  <!-- 全屏加载 - 粒子动画 -->
  <div v-if="type === 'fullscreen'" class="loading-container fullscreen">
    <div class="particle-loader">
      <div v-for="i in 12" :key="i" class="particle" :style="getParticleStyle(i)"></div>
    </div>
    <p class="loading-text">{{ text || '正在加载...' }}</p>
  </div>

  <!-- 内联加载 - 跳动圆点 -->
  <div v-else-if="type === 'inline'" class="loading-container inline">
    <div class="jumping-dots">
      <div class="dot"></div>
      <div class="dot"></div>
      <div class="dot"></div>
    </div>
    <span class="loading-text">{{ text || '加载中' }}</span>
  </div>

  <!-- 骨架屏加载 - 渐变闪烁 -->
  <div v-else-if="type === 'skeleton'" class="loading-container skeleton">
    <div class="skeleton-card">
      <div class="skeleton-avatar"></div>
      <div class="skeleton-line"></div>
      <div class="skeleton-line"></div>
      <div class="skeleton-line short"></div>
    </div>
  </div>

  <!-- 进度条加载 - 流体效果 -->
  <div v-else-if="type === 'progress'" class="loading-container progress">
    <div class="fluid-progress">
      <div class="progress-track" :style="{ width: progress + '%' }">
        <div class="progress-wave"></div>
      </div>
    </div>
    <span class="progress-text">{{ progress }}%</span>
  </div>

  <!-- 默认加载 - 3D旋转方块 -->
  <div v-else class="loading-container default">
    <div class="cube-loader">
      <div class="cube"></div>
      <div class="cube"></div>
      <div class="cube"></div>
      <div class="cube"></div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';

defineProps({
  type: {
    type: String,
    default: 'default',
    validator: (value) => ['fullscreen', 'inline', 'skeleton', 'progress', 'default'].includes(value)
  },
  text: {
    type: String,
    default: ''
  },
  progress: {
    type: Number,
    default: 0,
    validator: (value) => value >= 0 && value <= 100
  }
});

// 粒子位置计算
const getParticleStyle = (index) => {
  const angle = (index * 30) * (Math.PI / 180);
  const distance = 30;
  return {
    transform: `rotate(${index * 30}deg) translate(${distance}px) rotate(-${index * 30}deg)`,
    animationDelay: `${index * 0.1}s`
  };
};
</script>

<style scoped lang="scss">
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;

  &.fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 255, 0.95);
    z-index: 9999;
    flex-direction: column;
    backdrop-filter: blur(2px);

    .loading-text {
      margin-top: 2rem;
      font-size: 1.2rem;
      color: #4a6bff;
      font-weight: 500;
    }
  }

  &.inline {
    gap: 0.75rem;
    padding: 0.75rem;

    .loading-text {
      font-size: 0.9rem;
      color: #666;
    }
  }

  &.skeleton {
    padding: 1.5rem;
    width: 100%;
  }

  &.progress {
    flex-direction: column;
    width: 100%;
    gap: 0.75rem;

    .progress-text {
      font-size: 0.9rem;
      color: #4a6bff;
      font-weight: 500;
    }
  }
}

/* 粒子加载动画 */
.particle-loader {
  position: relative;
  width: 60px;
  height: 60px;

  .particle {
    position: absolute;
    width: 10px;
    height: 10px;
    background: #4a6bff;
    border-radius: 50%;
    animation: particlePulse 1.2s infinite ease-in-out;
    opacity: 0;

    @keyframes particlePulse {

      0%,
      100% {
        transform: scale(0.5);
        opacity: 0;
      }

      50% {
        transform: scale(1);
        opacity: 1;
      }
    }
  }
}

/* 跳动圆点动画 */
.jumping-dots {
  display: flex;
  align-items: center;
  height: 20px;

  .dot {
    width: 10px;
    height: 10px;
    margin: 0 3px;
    background-color: #4a6bff;
    border-radius: 50%;
    animation: jump 1s infinite ease-in-out;

    &:nth-child(1) {
      animation-delay: 0s;
    }

    &:nth-child(2) {
      animation-delay: 0.2s;
    }

    &:nth-child(3) {
      animation-delay: 0.4s;
    }

    @keyframes jump {

      0%,
      100% {
        transform: translateY(0);
      }

      50% {
        transform: translateY(-10px);
      }
    }
  }
}

/* 骨架屏动画 */
.skeleton-card {
  width: 100%;
  max-width: 300px;
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);

  .skeleton-avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    margin-bottom: 1rem;
    animation: skeletonShimmer 1.5s infinite linear;
  }

  .skeleton-line {
    height: 12px;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    border-radius: 6px;
    margin-bottom: 0.75rem;
    animation: skeletonShimmer 1.5s infinite linear;

    &.short {
      width: 70%;
    }
  }

  @keyframes skeletonShimmer {
    0% {
      background-position: 200% 0;
    }

    100% {
      background-position: -200% 0;
    }
  }
}

/* 流体进度条 */
.fluid-progress {
  width: 100%;
  height: 8px;
  background: #f0f0f0;
  border-radius: 4px;
  overflow: hidden;

  .progress-track {
    height: 100%;
    background: linear-gradient(90deg, #4a6bff, #6d8bff);
    position: relative;
    transition: width 0.4s cubic-bezier(0.65, 0, 0.35, 1);

    .progress-wave {
      position: absolute;
      top: -3px;
      left: 0;
      right: 0;
      height: 14px;
      background: url("data:image/svg+xml,%3Csvg viewBox='0 0 1200 120' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0,0V46.29c47.79,22.2,103.59,32.17,158,28,70.36-5.37,136.33-33.31,206.8-37.5C438.64,32.43,512.34,53.67,583,72.05c69.27,18,138.3,24.88,209.4,13.08,36.15-6,69.85-17.84,104.45-29.34C989.49,25,1113-14.29,1200,52.47V0Z' opacity='.25' fill='%234a6bff'/%3E%3Cpath d='M0,0V15.81C13,36.92,27.64,56.86,47.69,72.05,99.41,111.27,165,111,224.58,91.58c31.15-10.15,60.09-26.07,89.67-39.8,40.92-19,84.73-46,130.83-49.67,36.26-2.85,70.9,9.42,98.6,31.56,31.77,25.39,62.32,62,103.63,73,40.44,10.79,81.35-6.69,119.13-24.28s75.16-39,116.92-43.05c59.73-5.85,113.28,22.88,168.9,38.84,30.2,8.66,59,6.17,87.09-7.5,22.43-10.89,48-26.93,60.65-49.24V0Z' opacity='.5' fill='%234a6bff'/%3E%3Cpath d='M0,0V5.63C149.93,59,314.09,71.32,475.83,42.57c43-7.64,84.23-20.12,127.61-26.46,59-8.63,112.48,12.24,165.56,35.4C827.93,77.22,886,95.24,951.2,90c86.53-7,172.46-45.71,248.8-84.81V0Z' fill='%234a6bff'/%3E%3C/svg%3E");
      background-size: 1200px 100%;
      animation: wave 8s linear infinite;
      opacity: 0.6;
    }
  }

  @keyframes wave {
    0% {
      background-position-x: 0;
    }

    100% {
      background-position-x: 1200px;
    }
  }
}

/* 3D旋转方块动画 */
.cube-loader {
  width: 40px;
  height: 40px;
  position: relative;
  transform-style: preserve-3d;
  animation: cubeRotate 3s infinite ease-in-out;

  .cube {
    position: absolute;
    width: 100%;
    height: 100%;
    background: rgba(74, 107, 255, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 0 10px rgba(74, 107, 255, 0.4);

    &:nth-child(1) {
      transform: rotateX(90deg) translateZ(20px);
    }

    &:nth-child(2) {
      transform: rotateX(-90deg) translateZ(20px);
    }

    &:nth-child(3) {
      transform: rotateY(90deg) translateZ(20px);
    }

    &:nth-child(4) {
      transform: rotateY(-90deg) translateZ(20px);
    }
  }

  @keyframes cubeRotate {

    0%,
    100% {
      transform: rotateX(0) rotateY(0);
    }

    25% {
      transform: rotateX(90deg) rotateY(0);
    }

    50% {
      transform: rotateX(90deg) rotateY(90deg);
    }

    75% {
      transform: rotateX(0) rotateY(90deg);
    }
  }
}
</style>